/*
 * @(#)GradientShapePainter.java	1.0 Apr 27, 2008
 *
 *	The MIT License
 *
 *	Copyright (c) 2008 Malachi de AElfweald <malachid@gmail.com>
 *
 *	Permission is hereby granted, free of charge, to any person obtaining a copy
 *	of this software and associated documentation files (the "Software"), to deal
 *	in the Software without restriction, including without limitation the rights
 *	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 *	copies of the Software, and to permit persons to whom the Software is
 *	furnished to do so, subject to the following conditions:
 *
 *	The above copyright notice and this permission notice shall be included in
 *	all copies or substantial portions of the Software.
 *
 *	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 *	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 *	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 *	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 *	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 *	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 *	THE SOFTWARE.
 */
package org.eoti.swing.paint;

import org.eoti.awt.ColorUtil;
import org.eoti.awt.WebColor;

import java.awt.*;

public class GradientShapePainter
extends ShapePainter
{
	public enum Location{North,NorthEast,East,SouthEast,South,SouthWest,West,NorthWest,Center}
	protected Location gradientLoc1, gradientLoc2;

	public GradientShapePainter()
	{
		setBackground(WebColor.DarkSlateGray.getColor());
		setForeground(WebColor.DarkSeaGreen.getColor());
		setLocation1(Location.NorthWest);
		setLocation2(Location.SouthEast);
	}

	public void setLocation1(Location gradientLoc1){this.gradientLoc1 = gradientLoc1;}
	public Location getLocation1(){return gradientLoc1;}

	public void setLocation2(Location gradientLoc2){this.gradientLoc2 = gradientLoc2;}
	public Location getLocation2(){return gradientLoc2;}

	public void paint(Graphics g, Shape shape, Component source, int x, int y, int width, int height)
	{
		Graphics2D g2 = (Graphics2D)g;
		float x1 = 0;
		float x2 = 0;
		float y1 = 0;
		float y2 = 0;
		switch(gradientLoc1)
		{
			case NorthWest:
			case West:
			case SouthWest:
				x1 = x;
				break;
			case North:
			case Center:
			case South:
				x1 = x + (width / 2);
				break;
			case NorthEast:
			case East:
			case SouthEast:
				x1 = x + width;
				break;
		}
		switch(gradientLoc1)
		{
			case NorthWest:
			case North:
			case NorthEast:
				y1 = y;
				break;
			case West:
			case Center:
			case East:
				y1 = y + (height / 2);
				break;
			case SouthWest:
			case South:
			case SouthEast:
				y1 = y + height;
				break;
		}
		switch(gradientLoc2)
		{
			case NorthWest:
			case West:
			case SouthWest:
				x2 = x;
				break;
			case North:
			case Center:
			case South:
				x2 = x + (width / 2);
				break;
			case NorthEast:
			case East:
			case SouthEast:
				x2 = x + width;
				break;
		}
		switch(gradientLoc2)
		{
			case NorthWest:
			case North:
			case NorthEast:
				y2 = y;
				break;
			case West:
			case Center:
			case East:
				y2 = y + (height / 2);
				break;
			case SouthWest:
			case South:
			case SouthEast:
				y2 = y + height;
				break;
		}

		g2.setPaint(
			new GradientPaint(
				x1,
				y1,
				getBackground(),
				x2,
				y2,
				getForeground()
			)
		);
		g2.fill(shape);
	}
}
